*Command to generate consecutive spells of Judge-Courts, will use output to generate other variables of interest
tsspell Judge_Court_idP		
													
/*Creates _seq with month in each spell and _end, an indicator for end of spell*/

sort Judge_Court_idP YM
gegen maxrun = max(_seq), by(Judge_Court_idP YM)				//longest sequence for judge-court
by Judge_Court_idP: replace maxrun = . if _n != 1

gen unique_spell = 1 if _end==1
gen duration_spell = _seq if _end==1											//duration of each spell
gegen spell_ident = group(Judge_Court_idP _spell)
sort spell_ident
gegen duration_spell_tot = max(duration_spell), by(spell_ident)					

sort Judge_idP
gegen maxrun_judge = max(maxrun), by(Judge_idP)								//longest sequence for judge
by Judge_idP: replace maxrun_judge = . if _n !=1

sort Judge_idP YM
clonevar last_date = YM
clonevar first_date = YM
by Judge_idP: replace first_date = . if _n!=1
by Judge_idP: replace last_date = .  if _n!=_N


*Unique number of months per judge_court pair
qui unique YM, by(Judge_Court_idP) gen(period_by_judge_court)
qui unique YM, by(Judge_idP) gen(period_by_judge)

bys Judge_idP YM: gen total_courts = _N
bys Court_idP YM: gen total_judges = _N

*number of months (not necessarily consecutive) in one court, by judge-court-month
sort Judge_Court_idP YM
by Judge_Court_idP: gen months_judge_court = _n

*Describing Unique obs
qui unique Judge_idP, by(Judge_idP) gen(Unique_Judge)
qui unique Court_idP, by(Judge_idP) gen(court_by_judge)
recode court_by_judge (1 = .), gen(judge_multiple)

*Number of Courts a judge work in a given month
gegen Judge_YM = group(Judge_idP YM)
qui unique Court_idP, by(Judge_YM) gen(courts_month)

qui unique Judge_Court_idP, by(Judge_Court_idP) gen(Unique_Judge_Court)

qui unique Court_idP, by(Court_idP) gen(Unique_Court)

*how many judicial districts a judge works in a given month
qui unique Comarca_idP, by(Judge_YM) gen(district_month)
gegen max_districts = max(district_month), by(Judge_idP)
bys Judge_idP: replace max_districts = . if _n!=1

*Unique courts per judge, and vice versa; also unique judges(courts) with multiple courts(judges)
qui unique Judge_idP, by(Court_idP) gen(judge_by_court)
recode judge_by_court (1 = .), gen(court_multiple)

*Number of Judges working on a court in a given month
gegen Court_YM = group(Court_idP YM)
qui unique Judge_idP, by(Court_YM) gen(judges_month)
bys Court_YM: gen Court_YM_unique = 1 if _n==1
gen Allocated_C_court =  Allocated_C if Court_YM_unique==1
gen Process_Ongoing_C_court = Process_Ongoing_C if Court_YM_unique==1
gen Process_Await_Compliance_C_court = Process_Await_Compliance_C  if Court_YM_unique==1
gen Process_Await_Progress_C_court = Process_Await_Progress_C if Court_YM_unique==1


*Unique number of municipalities and courts per municipalities
qui unique Municipality_enc, by(Municipality_enc) gen(Unique_munic)
qui unique Court_idP, by(Municipality_enc) gen(court_by_munic)

*Unique municipalities a judge ever works in
qui unique Comarca_idP, by(Judge_idP) gen(Unique_munic_judge)

*Type of courts
bys Court_idP: gen type_court_short_1_f = type_court_short_1 if _n == 1
bys Court_idP: gen type_court_short_2_f = type_court_short_2 if _n == 1
bys Court_idP: gen type_court_short_3_f = type_court_short_3 if _n == 1
bys Court_idP: gen type_court_short_4_f = type_court_short_4 if _n == 1
bys Court_idP: gen type_court_short_5_f = type_court_short_5 if _n == 1
bys Court_idP: gen type_court_short_6_f = type_court_short_6 if _n == 1

*Level of courts
bys Court_idP: gen court_level_1_f = court_level_1 if _n == 1
bys Court_idP: gen court_level_2_f = court_level_2 if _n == 1
bys Court_idP: gen court_level_3_f = court_level_3 if _n == 1
bys Court_idP: gen court_level_4_f = court_level_4 if _n == 1
bys Court_idP: gen court_level_5_f = court_level_5 if _n == 1

*Judge Gender
bys Judge_idP: gen gender_f = gender if _n==1
gen male_f = (gender_f == "M") if gender_f!=""
bys Judge_idP: gen promotion_max_f = promotion_max if _n == 1

lab var Unique_Judge "Unique Judges"
lab var court_by_judge "\# Courts by Judge"
lab var judge_multiple "\# Judges ever work in multiple Courts"
lab var courts_month "\# of Courts at Judge-Month level"
lab var Unique_Judge_Court "Unique Judge-court pairs"
lab var Unique_Court "Number of unique Courts"
lab var judge_by_court "\# Judges by Court"
lab var court_multiple "\# Courts with multiple Judges"
lab var judges_month "\# of Judges at Court-Month level"
lab var period_by_judge_court "Unique number of months per judge-court pair"
lab var Unique_munic "\# of Municipalities"
lab var court_by_munic "\# of Courts per Municipality"
lab var unit "Number of Observations"
lab var maxrun "Longest spell at Judge-Court level"
lab var maxrun_judge "Longest spell for a Judge"
lab var unique_spell "Unique Judge-Court spells"
lab var duration_spell "Duration of unique spells"
lab var period_by_judge "Number of months Judge is observed"
lab var first_date "First month a judge is observed"
lab var last_date "Last month a judge is observed"
lab var Allocated_C_court "Processes Allocated (Court-Month)"
lab var Process_Ongoing_C_court "Processes Ongoing (Court-Month)"
lab var Process_Await_Compliance_C_court "Processes wait compliance 100+ days (Court-Month)"
lab var Process_Await_Progress_C_court "Processes wait progress 100+ days (Court-Month)"
lab var district_month "\# Judicial Districts Judge works in"
lab var max_districts "Max \# of districts Judge works in month"
lab var Unique_munic_judge "Number Municipalities Judge ever works in"
lab var type_court_short_1_f "General Court"
lab var type_court_short_2_f "Criminal Court"
lab var type_court_short_3_f "Civil Court"
lab var type_court_short_4_f "Family Court"
lab var type_court_short_5_f "Small-stakes Court"
lab var type_court_short_6_f "Other Courts"
lab var male_f  			 "Male"
lab var promotion_max_f 	 "Judge ever promoted"
lab var court_level_1_f "Initial Court"
lab var court_level_2_f "Intermediate Court"
lab var court_level_3_f "Final Court"
lab var court_level_4_f "Unique Court"
lab var court_level_5_f "Special Court"
